﻿var Q_event = {
    target: function(e) {
        // for Mozilla/Internet Explorer
        if(e.target) return e.target;
        else if(e.srcElement) return e.srcElement;
    }
    ,add: function(objct,type,fnctn) {
        // for Mozilla/Internet Explorer
        if (objct.addEventListener) {
            objct.addEventListener(type,fnctn,false); return true;
        } else if (objct.attachEvent) {
            var rtrn = objct.attachEvent('on'+type,fnctn); return rtrn;
        } else {
            objct.onclick = fnctn; //objct['on'+type] = fnctn;
        }
    }
    ,remove: function(objct,type,fnctn) {
        // for Mozilla/Internet Explorer
        if(objct.removeEventListener) {
            objct.removeEventListener(type,fnctn,false); return true;
        } else if(objct.detachEvent) {
            var rtrn = objct.detachEvent('on'+type,fnctn); return rtrn;
        } else {
            objct.onclick = null; //objct['on'+type] = null;
        }
    }
};

var Q_Drag = {
     optn: null //resize_x|resize_y|resize_a
    ,s_clntX: null
    ,s_clntY: null
    ,x_lmt : null
    ,y_lmt : null
    ,div_w: null
    ,div_h: null
    ,element:null
    ,elements:null
    ,WidgetID:null
    ,dragstart: function() { return false; }
    ,mousemove: function(e) {
        // for Internet Explorer
        if(!e) e = window.event;
        var delta_x = e.clientX-Q_Drag.s_clntX;
        var delta_y = e.clientY-Q_Drag.s_clntY;
        //window.status = "Drag2:" + delta_x+'/'+Q_Drag.x_lmt+':'+delta_y+'/'+Q_Drag.y_lmt + ":" + Q_Drag.optn + ":" + Q_Drag.div_w + "/" + Q_Drag.div_h;
        if(Q_Drag.optn=='resize_x' || Q_Drag.optn=='resize_a') {
            var frst_x = Q_Drag.div_w+delta_x;
            if(0<frst_x && frst_x<Q_Drag.x_lmt) {
               var tmp = Q_Drag.div_w+delta_x+'px';
                Q_Drag.element.style.width = tmp;
                for(var i=0;i<Q_Drag.elements.length;i++)
                {
                 if (Q_Drag.elements[i])
                     Q_Drag.elements[i].style.width = tmp;
                }
               //window.status += ":Width: " + Q_Drag.element.style.width;
            }
        }
        if(Q_Drag.optn=='resize_y' || Q_Drag.optn=='resize_a') {
            var frst_y = Q_Drag.div_h+delta_y;
            if(0< frst_y && frst_y<Q_Drag.y_lmt) {
               var tmp = Q_Drag.div_h+delta_y+'px';
                Q_Drag.element.style.height = tmp;
                for(var i=0;i<Q_Drag.elements.length;i++)
                {
                  if (Q_Drag.elements[i])
                     Q_Drag.elements[i].style.height = tmp;
                }
      
                //window.status += ":Height:" + Q_Drag.element.style.height;
            }
        }
    }
    ,mouseup: function(e) {
         var theHeight = Q_Drag.element.style.height;
         var theWidget = $WI(Q_Drag.WidgetID);
         if (theWidget.ResizeHeightComplete)
            theWidget.ResizeHeightComplete(theHeight);
          theWidget.SaveHeight(theHeight);
        Q_event.remove(document,'mousemove',Q_Drag.mousemove);
        Q_event.remove(document,'mouseup'  ,Q_Drag.mouseup);
    }
    ,start: function(e,optn, element, WidgetID) {
        Q_Drag.optn = optn;
        Q_Drag.s_clntX = e.clientX;
        Q_Drag.s_clntY = e.clientY;
        Q_Drag.WidgetID = WidgetID;        
        Q_Drag.elements = new Array();
        if (element.indexOf("|") > -1)
        {
           var i =0;
           var Elements = element.split("|");
           for(var i=0;i<Elements.length;i++)
           {
               var ElementItem = Elements[i];
               if (i==0)
               {
                  Q_Drag.element = $get(ElementItem);
               }
               else
               {
                  Q_Drag.elements.push($get(ElementItem));
               }
           }
        }
        else
        {        
           Q_Drag.element = document.getElementById(element);
        }
        
        Q_Drag.div_w = getElementWidth(Q_Drag.element);
        Q_Drag.div_h = getElementHeight(Q_Drag.element);
        Q_Drag.x_lmt = 32767;
        Q_Drag.y_lmt = 32767;
        // for Internet Explorer
        if(e.srcElement) Q_event.add(e.srcElement,'dragstart',Q_Drag.dragstart);
        Q_event.add(document,'mousemove',Q_Drag.mousemove);
        Q_event.add(document,'mouseup'  ,Q_Drag.mouseup);
    }
};